package com.zhyl.demo.controller;

import com.zhyl.demo.entity.Medication;
import com.zhyl.demo.service.MedicationService;
import com.zhyl.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @author yang3
 */
@RestController
@RequestMapping("/medication")
public class MedicationController {

    private final MedicationService medicationService;

    @Autowired
    private MedicationController(MedicationService medicationService)
    {
        this.medicationService = medicationService;
    }

    @RequestMapping("/getMedicList")
    public Result getMedicList() {
        ArrayList<Medication> medicList = medicationService.getMedicList();

        if (medicList != null) {
            return Result.success(medicList);
        } else {
            return Result.error("-1", "暂无数据");
        }
    }

    @RequestMapping("/getSelectMedic")
    public Result getSelectMedic(String medicName)
    {
        if (medicName==null){
            return Result.error("请求错误");
        }
        ArrayList<Medication> selectMedic = medicationService.getSelectMedic(medicName);
        if (selectMedic!=null)
        {
            return Result.success(selectMedic);
        }else {
            return Result.error("数据错误");
        }
    }

    @RequestMapping("/createMedic")
    public Result createMedic(@RequestBody Medication medication)
    {
        if (medication==null){
            return Result.error("-1","请求错误");
        }
        Medication medications = medicationService.checkMedic(medication.getName());
        if (medications!=null){
            return Result.error("-1","该药品已存在");
        }
        Boolean i = medicationService.createMedic(medication);
        if (i){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @RequestMapping("/updateMedic")
    public Result updateMedic(@RequestBody Medication medication)
    {
        if (medication==null){
            return Result.error("-1","请求错误");
        }
        Boolean i = medicationService.updateMedic(medication);
        if (i){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @RequestMapping("deleteMedic")
    public Result deleteMedic(Integer medId)
    {
        if (medId==null)
        {
            return Result.error("-1","请求错误");
        }
        Boolean i = medicationService.deleteMedic(medId);

        if (i){
            return Result.success("删除成功");
        }else {

            return Result.error("删除失败");
        }
    }

}
